/*
 * CManager.cpp
 *
 *  Created on: 2016-4-14
 *      Author: root
 */
#include"CMultiThreadManager.h"
#if USE_THREAD_MANAGER
#include"PublicTool.h"
#include"PublicLog.h"
#include<stdio.h>
#include<stdlib.h>
CMultiThreadManager::CMultiThreadManager(){
    strcpy(m_ThreadMode,"MODE_NONE");
    m_ThreadNum=0;
    m_ThreadRunning=0;
    m_ThreadInfoList=NULL;
}
CMultiThreadManager::~CMultiThreadManager(){

}
void*CMultiThreadManager::WorkThread(void*argv){
    pthread_t threadid=pthread_self();
//绑定CPU	
#if  (__GLIBC__ ==2) && (__GLIBC_MINOR__ < 4 )
	ThreadBindCPU(threadid,0);
#endif		
    CMultiThreadManager*pCBaseManager=(CMultiThreadManager*)argv;
    pCBaseManager->UpdateThreadStatus(threadid,THREAD_RUNNING);
    pCBaseManager->RunWorkThreadMain();
    pCBaseManager->UpdateThreadStatus(threadid,THREAD_STOPPED);
//已设置分离属性，使用exit会有内存泄漏
//  pthread_exit(NULL);
//  pthread_detach(threadid);
    return NULL;
}
void CMultiThreadManager::RunWorkThreadMain(){
    while(IsThreadRunningFlag()){
		m_func(m_usrdata);
    }
}
void CMultiThreadManager::StartWorkThread(thrfunc* funfunc,void*usrdata,int ThreadNum,const char*threadname){
    if(ThreadNum==0)return;
	if(threadname!=NULL)strcpy(m_ThreadMode,threadname);
	m_usrdata=usrdata;
	m_func=funfunc;
    m_ThreadNum=ThreadNum;
    m_ThreadRunning=1;
    m_ThreadInfoList=(ThreadInfo*)malloc(sizeof(ThreadInfo)*m_ThreadNum);
    memset(m_ThreadInfoList,0,sizeof(ThreadInfo)*m_ThreadNum);
    int i=0;
	m_Thread_Mutex.Lock();
    for(i=0;i<m_ThreadNum;i++){
		ThreadCreate(&m_ThreadInfoList[i].threadid,&WorkThread,this);
    }
	WARN("线程类型:%s 数量:%d 创建完毕",m_ThreadMode,m_ThreadNum);
	m_Thread_Mutex.UnLock();
    while(IsAllThreadsRunning()==false){
		sleep(2);
    }
    WARN("线程类型:%s 数量:%d 启动成功",m_ThreadMode,m_ThreadNum);
}
void CMultiThreadManager::StopWorkThread(){
    if(IsThreadRunningFlag()==false)return;
	m_Thread_Mutex.Lock();
    m_ThreadRunning=0;
	m_Thread_Mutex.UnLock();
    while(IsAllThreadsStopped()==false){
		sleep(3);
    }
#if 1
    int i=0;
    for(i=0;i<m_ThreadNum;i++){
		//pthread_join(m_ThreadInfoList[i].threadid,NULL);
		ThreadDestroy(m_ThreadInfoList[i].threadid);
		m_ThreadInfoList[i].threadid=0;
    }
#endif
    if(m_ThreadInfoList){
        memset(m_ThreadInfoList,0,sizeof(ThreadInfo)*m_ThreadNum);
        free(m_ThreadInfoList);
    }
    WARN("线程类型:%s 数量:%d 退出成功",m_ThreadMode,m_ThreadNum);
    m_ThreadInfoList=NULL;
    m_ThreadNum=0;
}
bool CMultiThreadManager::IsThreadRunningFlag(){
	bool bRunning=true;
	m_Thread_Mutex.Lock();
	bRunning=m_ThreadRunning==1;
	m_Thread_Mutex.UnLock();	
	return bRunning;
}
void CMultiThreadManager::UpdateThreadStatus(pthread_t threadid,ThreadStatus status){
    int i=0,bFind=0;
    m_Thread_Mutex.Lock();
    for(i=0;i<m_ThreadNum;i++){
        if(m_ThreadInfoList[i].threadid==threadid){
			m_ThreadInfoList[i].status=status;
			bFind=1;
			break;
        }
    }
    m_Thread_Mutex.UnLock();
    if(bFind==0)WRONG("线程类型:%s 未找到对应线程:%lu",m_ThreadMode,threadid);
}
bool CMultiThreadManager::IsAllThreadsRunning(){
    int i=0,RunNum=0,StopNum=0;
    m_Thread_Mutex.Lock();
    for(i=0;i<m_ThreadNum;i++){
		if(m_ThreadInfoList[i].status==THREAD_RUNNING){
			RunNum++;
		}else{
			StopNum++;
        }
    }
    m_Thread_Mutex.UnLock();
    INFO("线程类型:%s 运行数:%d 待运行数:%d 总创建数:%d",m_ThreadMode,RunNum,StopNum,m_ThreadNum);
    if(RunNum==m_ThreadNum) return true;
    return false;
}
bool CMultiThreadManager::IsAllThreadsStopped(){
    int i=0,RunNum=0,StopNum=0;
    m_Thread_Mutex.Lock();
    for(i=0;i<m_ThreadNum;i++){
		if(m_ThreadInfoList[i].status==THREAD_STOPPED){
			StopNum++;
		}else{
			RunNum++;
        }
    }
    m_Thread_Mutex.UnLock();
    INFO("线程类型:%s 运行数:%d 停止数:%d 总创建数:%d",m_ThreadMode,RunNum,StopNum,m_ThreadNum);
    if(StopNum==m_ThreadNum) return true;
    return false;
}
#endif
